package com.zaizai.service.impl;

import java.util.List;

import com.zaizai.domain.Role;
import com.zaizai.domain.dto.RegisterDto;
import com.zaizai.mapper.RoleMapper;
import com.zaizai.service.IRoleService;
import com.zaizai.utils.Md5Util;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 角色Service业务层处理
 *
 * @author zaiZai
 * @date 2024-10-21
 */
@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    /**
     * 查询角色
     *
     * @param id 角色主键
     * @return 角色
     */
    @Override
    public Role selectRoleById(Long id) {
        return roleMapper.selectRoleById(id);
    }

    /**
     * 查询角色列表
     *
     * @param role 角色
     * @return 角色
     */
    @Override
    public List<Role> selectRoleList(Role role) {
        return roleMapper.selectRoleList(role);
    }

    /**
     * 新增角色
     *
     * @param role 角色
     * @return 结果
     */
    @Override
    public int insertRole(Role role) {
        return roleMapper.insertRole(role);
    }

    /**
     * 修改角色
     *
     * @param role 角色
     * @return 结果
     */
    @Override
    public int updateRole(Role role) {
        return roleMapper.updateRole(role);
    }

    /**
     * 批量删除角色
     *
     * @param ids 需要删除的角色主键
     * @return 结果
     */
    @Override
    public int deleteRoleByIds(Long[] ids) {
        return roleMapper.deleteRoleByIds(ids);
    }

    /**
     * 删除角色信息
     *
     * @param id 角色主键
     * @return 结果
     */
    @Override
    public int deleteRoleById(Long id) {
        return roleMapper.deleteRoleById(id);
    }

    @Override
    public Integer register(RegisterDto registerDto) {
        //先检验账号是否重复
        Role u = roleMapper.findUserByUsername(registerDto.getUserName());
        //账号不重复
        if (u == null) {
            //判断密码是否一致
            if (registerDto.getPassword().equals(registerDto.getRePassword())) {
                //判断手机号是否重复
                if (roleMapper.findUserByTelephone(registerDto.getTelephone()) == null) {
                    //手机号不重复
                    //密码一致
                    Role user = new Role();
                    BeanUtils.copyProperties(registerDto, user);
                    //密码加密
                    user.setPassword(Md5Util.getMD5String(registerDto.getPassword()));
                    roleMapper.insertRole(user);
                    return 1;
                }
                return -3; //手机号重复
            }
            return -1; //密码不一致
        }
        return -2; //账号重复
    }


    @Override
    public Role login(RegisterDto registerDto) {
        //查询账号
        Role user = roleMapper.findUserByUsername(registerDto.getUserName());
        //判断账号是否存在
        if (user != null) {
            //账号存在，判断密码是否一致
            if (Md5Util.checkPassword(registerDto.getPassword(), user.getPassword())) {
                //密码一致，登录成功
                return user;
            }
        }
        return null;
    }

    @Override
    public Role findRoleByEmail(String email) {
        return roleMapper.findRoleByEmail(email);
    }
}
